package com.his.struts.action;

import java.sql.ResultSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.his.struts.dao.type.TypeDao;
import com.his.struts.dao.type.TypeImp;
import com.his.struts.form.TypeForm;
import com.his.struts.util.AjaxUtil;
import com.his.struts.util.PageUtil;
import com.his.struts.util.TranCharset;

/** 
 * MyEclipse 肖伟亮
 * Creation date: 05-29-2011
 * 
 * 
 */
public class TypeAction extends DispatchAction {
	 
	TypeDao dao = new TypeImp();

	/** 
	 * Method getKeshi 得到所有科室
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward null
	 */
	public ActionForward getKeshi(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// 得到所有科室
		List<TypeForm> keshi = dao.getKeshi();
		// ajax发送数据
		AjaxUtil.send(response,keshi);
		
		return null;
	}
	
	/** 
	 * Method getKebie 该科室的所有可别
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward null
	 */
	public ActionForward getKebie(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		TypeForm typeForm = (TypeForm) form;
		// 得到该科室的所有科别
		List<TypeForm> kebie = dao.getKebie(typeForm.getKs_id());
		// ajax发送数据
		AjaxUtil.send(response,kebie);
		return null;
	}
	
	/** 
	 * Method getBing 该科别的所有病
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward null
	 */
	public ActionForward getBing(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		TypeForm typeForm = (TypeForm) form;
		int kb_id = typeForm.getKb_id();
		
		// 得到该科别的所有病
		List<TypeForm> bing = dao.getB_name(kb_id);
		// ajax发送数据
		AjaxUtil.send(response,bing);
		return null;
	}
	

	/** 
	 * Method allDisease 所有疾病
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 所有疾病页
	 */
	public ActionForward allDisease(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		String sql= "keshi s inner join kebie b on s.ks_id = b.ks_id " +
		"inner join bing d on b.kb_id = d.kb_id order by s.ks_id";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 20, request);
		
		if(rs != null){
			 // 封装
			List<TypeForm> allDisease = dao.getBean(rs);
			// 数据放到request中
			request.setAttribute("allDisease", allDisease);
		}
		return mapping.findForward("allDisease");
	}
	
	/** 
	 * Method deleteDisease 删除疾病
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 所有疾病页
	 */
	public ActionForward deleteDisease(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		TypeForm typeForm = (TypeForm) form;
		int bid = typeForm.getB_id();
		/*
		 * 如果bid 为空 跳到登陆
		 */
		if(bid == 0){
			return mapping.findForward("login");
		}
		// 根据id 删除该疾病
		 dao.deleteDisease(bid);
		 request.setAttribute("message", "删除成功");
		return mapping.findForward("allDisease2");
	}
	
	/** 
	 * Method addKeshi 添加科室
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 所有疾病页
	 */
	public ActionForward addKeshi(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		TypeForm typeForm = (TypeForm) form;
		// 取出科室名
		String ks_name = typeForm.getKs_name();
		String kb_name = typeForm.getKb_name();
		String b_name = typeForm.getB_name();
		String b_symptom = typeForm.getB_symptom();
		// 如果科室为空 跳到所有疾病网页
		if(ks_name == null || kb_name == null || b_name == null){
			return mapping.findForward("allDisease2");
		}
		// 添加科室
		int ks_id = dao.addKeshi(ks_name);
		// 添加科别
		int kb_id = dao.addKebie(ks_id, kb_name);
		// 添加疾病
		dao.addJibing(kb_id, b_name,b_symptom);
		request.setAttribute("message", "添加成功");
		return mapping.findForward("allDisease2");
	}
	
	/** 
	 * Method addKebie 添加科别
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 所有疾病页
	 */
	public ActionForward addKebie(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		TypeForm typeForm = (TypeForm) form;
		// 取出科别名
		String kb_name = typeForm.getKb_name();
		String b_name = typeForm.getB_name();
		int ks_id = typeForm.getKs_id();
		String b_symptom = typeForm.getB_symptom();
		// 如果科室为空 跳到所有疾病网页
		if(kb_name == null || ks_id == 0){
			return mapping.findForward("allDisease2");
		}
		// 添加科别
		int kb_id = dao.addKebie(ks_id,kb_name);
		// 添加疾病
		dao.addJibing(kb_id, b_name,b_symptom);
		request.setAttribute("message", "添加成功");
		return mapping.findForward("allDisease2");
	}
	
	/** 
	 * Method addJibing 添加疾病
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 所有疾病页
	 */
	public ActionForward addJibing(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		TypeForm typeForm = (TypeForm) form;
		// 取出科室名
		String b_name = typeForm.getB_name();
		int kb_id = typeForm.getKb_id();
		String b_symptom = typeForm.getB_symptom();
		// 如果科室为空 跳到所有疾病网页
		if(b_name == null || kb_id == 0){
			return mapping.findForward("allDisease2");
		}
		// 添加疾病
		dao.addJibing(kb_id, b_name,b_symptom);
		request.setAttribute("message", "添加成功");
		return mapping.findForward("allDisease2");
	}
	/** 
	 * Method 所有科別
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward getAllKb(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		TypeForm typeForm = (TypeForm) form;
		// 得到该科室的所有科别
		List<TypeForm> kebie = dao.getKebie();
		// 强转为json格式
		AjaxUtil.send(response,kebie);
		return null;
	}
	
	/** 
	 * Method 搜索疾病
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward searchDisease(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		TypeForm typeForm = (TypeForm) form;
		String b_name=typeForm.getB_name();
		String encoding = TranCharset.getEncoding(b_name);
		if("ISO-8859-1".equals(encoding)){
		b_name = TranCharset.TranEncodeTOUTF_8(b_name);
		}
		String sql= "bing where b_name like '%"+b_name.trim()+"%'";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 15, request);
		
		if(rs != null){
			 // 封装
			List<TypeForm> disease = dao.getDiseaseBean(rs);
			// 数据放到request中
			request.setAttribute("disease", disease);
			request.setAttribute("b_name", b_name);
		}
		String doc = request.getParameter("doc");
		if("wel".equals(doc)){
			return mapping.findForward("docSearchDisease");
		}
		return mapping.findForward("userSearchDisease");
	}
	
	/** 
	 * Method 更新疾病
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward updateDisease(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		TypeForm typeForm = (TypeForm) form;
		String b_name=typeForm.getB_name();
		String b_symptom = typeForm.getB_symptom();
		int b_id = typeForm.getB_id();
		dao.updateDisease(b_id, b_name,b_symptom);
		request.setAttribute("message", "更新成功");
		return mapping.findForward("allDisease2");
	}
	
}